package com.wcl.nullmusic.ui.fragment;

import android.os.AsyncTask;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.android.volley.Response;
import com.google.gson.Gson;
import com.wcl.nullmusic.R;
import com.wcl.nullmusic.base.BaseFragment;
import com.wcl.nullmusic.mode.Recommend_CategoryContents_List_List;
import com.wcl.nullmusic.mode.SongmenuResult;
import com.wcl.nullmusic.mode.db.NullMusic_DBTools;
import com.wcl.nullmusic.ui.activity.SongDetailActivity;
import com.wcl.nullmusic.ui.adapter.SongMenuRecyclerViewAdapter;
import com.wcl.nullmusic.utils.Contant;
import com.wcl.nullmusic.utils.NetworkUtils;
import com.wcl.nullmusic.utils.VolleyUtils;

import java.util.List;

/**
 * Created by dllo on 16/1/5.
 */
public class SongMenuFragment extends BaseFragment {
    private RecyclerView recyclerView;
    private SongMenuRecyclerViewAdapter adapter;
    private NullMusic_DBTools dbTools;

    @Override
    protected int getLayout() {
        return R.layout.fragment_songmenu;
    }

    @Override
    protected void getVaule() {

    }

    @Override
    protected void initView(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.songmenu_recyclerView);
        adapter = new SongMenuRecyclerViewAdapter(getActivity());
        dbTools = new NullMusic_DBTools(getActivity());
    }

    @Override
    protected void initData() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);

        loadData();
    }

    @Override
    protected void listener() {

    }

    private void loadData() {
        if (NetworkUtils.isConnected())
            VolleyUtils.getInstance().StringRequest(Contant.SONGMENU_RECYCLERVIEW_URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Gson gson = new Gson();
                    SongmenuResult result = gson.fromJson(response, SongmenuResult.class);
                    contents_list_lists = result.getList();
                    adapter.addData(contents_list_lists);
                    saveDatsa(contents_list_lists);
                }
            }, "Songmenu_RecyclerView_StringRequest");
        else {
            QueryData queryData = new QueryData();
            queryData.execute();
        }
    }

    private void saveDatsa(final List<Recommend_CategoryContents_List_List> datas) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                dbTools.updateAll_SongMenu(datas);
            }
        }).start();
    }


    List<Recommend_CategoryContents_List_List> contents_list_lists;

    class QueryData extends AsyncTask<Void, Void, List> {
        @Override
        protected List doInBackground(Void... params) {

            contents_list_lists = dbTools.queryAll_SongMenu();
            return contents_list_lists;
        }

        @Override
        protected void onPreExecute() {
            Toast.makeText(getActivity(), "Song正在加载数据", Toast.LENGTH_SHORT).show();
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(List list) {
            adapter.addData(list);
            Toast.makeText(getActivity(), "Song数据加载完成", Toast.LENGTH_SHORT).show();

        }
    }
}
